package com.impetus.project.skilltest.client.pages;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.impetus.project.skilltest.client.stub.SecurityService;
import com.impetus.project.skilltest.client.stub.SecurityServiceAsync;
import com.impetus.project.skilltest.common.dataobject.Candidate;

public class LoginPage {
	
	private static final SecurityServiceAsync securityService = GWT.create(SecurityService.class);
	
	public boolean loadPage(PageData pageData)
	{
		try
		{
			final Label statusLabel = new Label();		
			final Label loginPrompt = new Label("Please Login");
		    final Grid grid = new Grid(3, 2);
		    final Label namePrompt = new Label("User Name");
		    final TextBox nameTextbox = new TextBox();
		    final Label passwordPrompt = new Label("Password");
		    final PasswordTextBox passwordTextbox = new PasswordTextBox();
		    final Button button = new Button("Login");
		    
		    if(pageData != null && pageData.getUser() != null
		    	&& pageData.getUser().getCandidateid() != null)
		    {
		    	nameTextbox.setText(pageData.getUser().getCandidateid());
		    }
	
		    // Create a handler for the sendButton and nameField
			class LoginHandler implements ClickHandler, KeyUpHandler {
				// Fired when the user clicks on the sendButton.
				public void onClick(ClickEvent event) {
					sendDataToServer();
				}
	
				// Fired when the user types in the nameField.
				public void onKeyUp(KeyUpEvent event) {
					if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
						sendDataToServer();
					}
				}
	
				// Send the name from the nameField to the server and wait for a response.
				private void sendDataToServer() {
					button.setEnabled(false);
					String userId = nameTextbox.getText();
					String password = passwordTextbox.getText();
					statusLabel.setText("Authenticating... please wait");
									
					securityService.login(userId, password, 
						new AsyncCallback<Candidate>() {
							public void onFailure(Throwable caught) {
								// Show the RPC error message to the user
								statusLabel.setText(caught.getMessage());
								button.setEnabled(true);
								nameTextbox.setFocus(true);
							}
							public void onSuccess(Candidate result) {
								// Route user to the next page
								PageData newPageData = new PageData();
								newPageData.setUser(result);
								PageRouter.loadPage(GUIConstants.PAGES.HOME_PAGE, newPageData);
							}
						});
				}
			}
			
			// Add a handler to send the name to the server
			LoginHandler handler = new LoginHandler();
			button.addClickHandler(handler);
			passwordTextbox.addKeyUpHandler(handler);
			
		    // We can add style names to widgets
			button.addStyleName("sendButton");
			loginPrompt.addStyleName("loginPrompt");
			statusLabel.addStyleName("statusLabel");
		    nameTextbox.addStyleName("nameField");
		    passwordTextbox.addStyleName("passwordField");
	
		    grid.setWidget(0, 0, namePrompt);
		    grid.setWidget(0, 1, nameTextbox);
	
		    grid.setWidget(1, 0, passwordPrompt);
		    grid.setWidget(1, 1, passwordTextbox);
	
		    grid.setWidget(2, 1, button);
		    
		    final VerticalPanel vpanel = new VerticalPanel();
		    vpanel.add(loginPrompt);
		    vpanel.add(statusLabel);
		    vpanel.add(grid);
		    vpanel.addStyleName("center");
		    
		    RootPanel.get().clear();
		    RootPanel.get().add(vpanel);
		    	
			// Focus the cursor on the name field when the app loads
		    nameTextbox.setFocus(true);
		    
		    return true;
		}
		catch(Throwable t)
		{
			t.printStackTrace();
			return false;
		}
	}
}
